package com.ruoyi.cems.task;


import com.ruoyi.cems.service.IBmsEmergencyNoticeService;
import com.ruoyi.cems.service.IBmsEmergencyVariableControlService;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 外呼定时任务
 * */

@Slf4j
@Component
public class TeamCallScheduling {

    @Autowired
    private IBmsEmergencyNoticeService bmsEmergencyNoticeService;

    @Autowired
    private IBmsEmergencyVariableControlService bmsEmergencyVariableControlService;

    @Autowired
    private ISysUserService sysUserService;

    @Scheduled(cron = "${call.scheduled.searchTaskCron}")
    public void searchCallTask(){
        log.info("外呼查询 触发 -->> start");
        bmsEmergencyNoticeService.syncCallResult();
        log.info("外呼查询 触发 -->> end");
    }
    @Scheduled(cron = "${orgUser.sync.syncUsersCron}")
    public void syncUsers(){
        log.info("用户同步 触发 -->> start");
        sysUserService.syncUsers(null);
        log.info("用户同步 触发 -->> end");
    }


    @Scheduled(cron = "${emergency.notice.statusUpdateCron}")
    public void statusUpdate(){
        long startTime = System.currentTimeMillis();
        log.info("任务状态更新 触发 -->> start");
        try {
            bmsEmergencyVariableControlService.updateBmsEmergencyVariableStatus();
            int count = bmsEmergencyNoticeService.updateBmsEmergencyNoticeStatus();
            log.info( count +"条任务状态更新 触发 -->> end, 耗时: {}ms", System.currentTimeMillis() - startTime);
        } catch (Exception e) {
            log.error("任务状态更新执行异常，耗时: {}ms", System.currentTimeMillis() - startTime, e);
            throw e;
        }
    }

}